package com.example.animations.activities;

import android.animation.LayoutTransition;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;

import com.example.animations.R;

public class OrientationChangeWRActivity extends AppCompatActivity {
    private ConstraintLayout _parentConstraint;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_orientation_change);
        _parentConstraint = findViewById(R.id.parent_constraint);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            connectLandscapeConstraints();
        }

        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            connectPortraitConstraints();
        }

        super.onConfigurationChanged(newConfig);
    }

    private void connectPortraitConstraints() {
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(_parentConstraint);

        constraintSet.clear(R.id.first_view);
        constraintSet.clear(R.id.second_view);
        _parentConstraint.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

        constraintSet.connect(R.id.first_view, ConstraintSet.RIGHT, R.id.parent_constraint, ConstraintSet.RIGHT, 0);
        constraintSet.connect(R.id.first_view, ConstraintSet.TOP, R.id.parent_constraint, ConstraintSet.TOP, 0);
        constraintSet.connect(R.id.first_view, ConstraintSet.LEFT, R.id.parent_constraint, ConstraintSet.LEFT, 0);
        int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
        constraintSet.constrainHeight(R.id.first_view, height);

        constraintSet.connect(R.id.second_view, ConstraintSet.RIGHT, R.id.parent_constraint, ConstraintSet.RIGHT, 0);
        constraintSet.connect(R.id.second_view, ConstraintSet.BOTTOM, R.id.parent_constraint, ConstraintSet.BOTTOM, 0);
        constraintSet.connect(R.id.second_view, ConstraintSet.LEFT, R.id.parent_constraint, ConstraintSet.LEFT, 0);
        constraintSet.connect(R.id.second_view, ConstraintSet.TOP, R.id.first_view, ConstraintSet.BOTTOM, 0);

        constraintSet.applyTo(_parentConstraint);
    }

    private void connectLandscapeConstraints() {
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(_parentConstraint);

        constraintSet.clear(R.id.first_view);
        constraintSet.clear(R.id.second_view);
        _parentConstraint.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

        constraintSet.connect(R.id.first_view, ConstraintSet.RIGHT, R.id.second_view, ConstraintSet.LEFT, 0);
        constraintSet.connect(R.id.first_view, ConstraintSet.TOP, R.id.parent_constraint, ConstraintSet.TOP, 0);
        constraintSet.connect(R.id.first_view, ConstraintSet.LEFT, R.id.parent_constraint, ConstraintSet.LEFT, 0);
        constraintSet.connect(R.id.first_view, ConstraintSet.BOTTOM, R.id.parent_constraint, ConstraintSet.BOTTOM, 0);

        constraintSet.connect(R.id.second_view, ConstraintSet.RIGHT, R.id.parent_constraint, ConstraintSet.RIGHT, 0);
        constraintSet.connect(R.id.second_view, ConstraintSet.BOTTOM, R.id.parent_constraint, ConstraintSet.BOTTOM, 0);
        constraintSet.connect(R.id.second_view, ConstraintSet.TOP, R.id.parent_constraint, ConstraintSet.TOP, 0);
        int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 350, getResources().getDisplayMetrics());
        constraintSet.constrainWidth(R.id.second_view, width);

        constraintSet.applyTo(_parentConstraint);
    }
}
